﻿using MMK.Paket_Tracker.Runtime;
using System.Windows.Controls;
using MMK.Paket_Tracker.GUI.WebServiceReference;
using System;
using MMK.Paket_Tracker.GUI.Views;
using Microsoft.Reporting.WinForms;
using System.ComponentModel;
using System.Threading.Tasks;
using MMK.Paket_Tracker.GUI.Models;
using System.Windows.Threading;
using System.Windows.Input;
using MMK.Paket_Tracker.Runtime.BusinessEntities;
using MMK.Paket_Tracker.Runtime.Commands;

namespace MMK.Paket_Tracker.GUI.ViewModels
{
    /// <summary>
    /// Class for SendPacketDataViewModel
    /// </summary>
    public class SendPacketDataViewModel : ViewModelBase
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="SendPacketDataViewModel"/> class.
        /// </summary>
        /// <param name="owner">The owner.</param>
        public SendPacketDataViewModel(SendPacketData owner)
        {
            this.owner = owner;
            PrepareCommands();

            data = new SendPacketDataModel();
            data.Message = "Daten werden übertragen...";

            task = Task<String>.Factory.StartNew(sendPacketData);
            dispTimer = new DispatcherTimer();
            dispTimer.Tick += new EventHandler(OnTimerTick);
            dispTimer.Interval = TimeSpan.FromSeconds(1);
            dispTimer.Start();
        }

        private SendPacketDataModel data;

        /// <summary>
        /// Gets or sets the data.
        /// </summary>
        /// <value>The data.</value>
        public SendPacketDataModel Data
        {
            get { return data; }
            set { data = value; }
        }


        private void OnTimerTick(object source, EventArgs e)
        {
            if (!task.IsCompleted)
            {
                return;
            }
            dispTimer.Stop();
            data.Message = "Daten sind übertragen!" + Environment.NewLine + "Der Paketaufkleber wird gedruckt...";

            ReportViewer pPreview = ((PrinterPreview)((SendPacketData)this.owner).windowsFormsHost1.Child).ReportView;
            pPreview.ProcessingMode = ProcessingMode.Local;
            pPreview.LocalReport.ReportPath = "PacketSticker.rdlc";
            pPreview.LocalReport.SetParameters(new ReportParameter("Receiver_Name", receiver.Vorname.Trim()+" "+receiver.Name.Trim()));
            pPreview.LocalReport.SetParameters(new ReportParameter("Receiver_Street_Number", receiver.Strasse.Trim()+" "+receiver.Hausnummer.Trim()));
            pPreview.LocalReport.SetParameters(new ReportParameter("Receiver_PC_Town", receiver.PLZ+" "+receiver.Ort.Trim()));
            pPreview.LocalReport.SetParameters(new ReportParameter("Sender_Name", sender.Vorname.Trim()+" "+sender.Name.Trim()));
            pPreview.LocalReport.SetParameters(new ReportParameter("Sender_Street_Number", sender.Strasse.Trim() + " " + sender.Hausnummer.Trim()));
            pPreview.LocalReport.SetParameters(new ReportParameter("Sender_PC_Town", sender.PLZ + " " + sender.Ort.Trim()));
            pPreview.LocalReport.SetParameters(new ReportParameter("Weight", weight.ToString() + " kg"));
            pPreview.LocalReport.SetParameters(new ReportParameter("Price", "€ "+price.ToString("F")));
            pPreview.LocalReport.SetParameters(new ReportParameter("Packet_ID", task.Result));
            pPreview.SetDisplayMode(DisplayMode.PrintLayout);
            pPreview.Show();
            data.Message = "Fertig!";

        }

        private String sendPacketData()
        {
            String result = null;

            WebservicesSoapClient client = new WebservicesSoapClient();
            sender = new Adresse();
            receiver = new Adresse();

            sender.Hausnummer = BusinessLogic.BusinessLogic.SenderModel.Number;
            sender.Name = BusinessLogic.BusinessLogic.SenderModel.LastName;
            sender.Ort = BusinessLogic.BusinessLogic.SenderModel.Town;
            sender.PLZ = BusinessLogic.BusinessLogic.SenderModel.PostCode.ToString();
            sender.Strasse = BusinessLogic.BusinessLogic.SenderModel.Street;
            sender.Vorname = BusinessLogic.BusinessLogic.SenderModel.FirstName;

            receiver.Hausnummer = BusinessLogic.BusinessLogic.ReceiverModel.Number;
            receiver.Name = BusinessLogic.BusinessLogic.ReceiverModel.LastName;
            receiver.Ort = BusinessLogic.BusinessLogic.ReceiverModel.Town;
            receiver.PLZ = BusinessLogic.BusinessLogic.ReceiverModel.PostCode.ToString();
            receiver.Strasse = BusinessLogic.BusinessLogic.ReceiverModel.Street;
            receiver.Vorname = BusinessLogic.BusinessLogic.ReceiverModel.FirstName;

            weight = BusinessLogic.BusinessLogic.GetWeightModel.BalValue;
            price = Double.Parse(BusinessLogic.BusinessLogic.GetWeightModel.Price);

            result = client.NewPaket(weight, price, sender, receiver);
            result = "*" + result + "*";
            
            return result;
        }

        private void PrepareCommands()
        {
            CommandBinding goOn = new CommandBinding(PTCommands.GoOn);

            goOn.Executed += goOn_Executed;
            goOn.CanExecute += goOn_CanExecute;

            owner.CommandBindings.Add(goOn);
        }

        void goOn_CanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = true;
        }

        void goOn_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            if (data.Message == "Fertig!")
            {
                BusinessLogic.BusinessLogic.SenderModel = null;
                BusinessLogic.BusinessLogic.GetWeightModel = null;
                BusinessLogic.BusinessLogic.ReceiverModel = null;
                
                NotifyMainWindow(new MainWindowNotification(((UserControl)sender).Name, null, (RoutedUICommand)e.Command));
            }
        }
        private Task<String> task;
        private Adresse sender;
        private Adresse receiver;
        private Double weight, price;
        private DispatcherTimer dispTimer;

    }
}
